package com.vue.cn.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vue.cn.common.http.AjaxResult;
import com.vue.cn.domin.Employee;
import com.vue.cn.service.EmployeeService;
import com.vue.cn.utils.excel.ExcelUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Part;
import java.beans.PropertyDescriptor;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private EmployeeService employeeService;

    @GetMapping(value = "{currentPage}/{pageSize}")
    public AjaxResult findAll(@PathVariable int currentPage, @PathVariable int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Employee> all = employeeService.findAll();
        PageInfo<Employee> pageInfo = new PageInfo<>(all);
        long total = pageInfo.getTotal();

        return AjaxResult.ok(all, total);
    }

    @GetMapping(value = "{id}")
    public AjaxResult findById(@PathVariable int id) {
        Employee employee = employeeService.findById(id);
        return AjaxResult.ok(employee);

    }

    @DeleteMapping(value = "{id}")
    public AjaxResult deleteById(@PathVariable int id) {
        int row = employeeService.deleteById(id);
        return AjaxResult.ok();
    }

    @PostMapping()
    public AjaxResult add(@RequestBody Employee employee) {
        int row = employeeService.add(employee);
        if (row > 0) {
            return AjaxResult.ok();
        }
        return AjaxResult.error();
    }

    @PutMapping()
    public AjaxResult edit(@RequestBody Employee employee) {
        System.out.println(employee);
        int row = employeeService.edit(employee);
        return AjaxResult.ok();
    }



    @GetMapping("export")
    public ResponseEntity<byte[]> exportXlsx() throws IllegalAccessException, IOException {
        List<Employee> all = employeeService.findAll();
        Workbook workbook = excelUtils.listToExcel(all, "员工信息表");

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        byte[] bytes = outputStream.toByteArray();

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", URLEncoder.encode("员工信息表.xlsx", "utf-8"));

        return new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);

    }


    @PostMapping("import")
    public AjaxResult importXlsx(@RequestPart("form") Part part) throws Exception {
        Workbook workbook = new XSSFWorkbook(part.getInputStream());

        List<Employee> employees = excelUtils.excelToList(workbook, Employee.class);
        employees.forEach(employee -> employeeService.add(employee));

        return AjaxResult.ok();
    }




}
